package tt.dz.appserver.util.encoding;

import java.io.UnsupportedEncodingException;
import java.util.Random;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * 编码帮助类
 * 
 * @author 谢龙飞
 *
 */
public class EncodingUtils {

	/**
	 * 获取随机字符串
	 * 
	 * @return
	 */
	public static final String getRandomString(int length) {

		// 拼接对象
		StringBuffer suffix = new StringBuffer();
		// 15个长度的字符
		for (int i = 0; i < length; i++) {
			int ch = new Random().nextInt(3);
			switch (ch) {
			// A-Z
			case 0:
				suffix.append((char) (Math.random() * 25 + 97));
				break;
			// 0-9
			case 1:
				suffix.append((int) (Math.random() * 9));
				break;
			// a-z
			default:
				suffix.append((char) (Math.random() * 25 + 65));
				break;
			}
		}
		return suffix.toString();
	}
	
	/**
	 * 获取随机字符串
	 * 
	 * @return
	 */
	public static final String getRandomIntegerString(int length) {

		StringBuffer suffix = new StringBuffer();
		for (int i = 0; i < length; i++) {
			suffix.append((int) (Math.random() * 9));
		}
		return suffix.toString();
	}

	/**
	 * 生成散列字符
	 * 
	 * @param randomCharSequence
	 *            随机字符
	 * @param phone
	 *            手机号码
	 * @return
	 */
	public static final String getToken(String randomCharSequence, String phone) {

		String tokenBeforeBase;
		try {
			// 散列
			tokenBeforeBase = MD5(MD5(randomCharSequence + phone) + randomCharSequence);
			// token
			return randomCharSequence + Base64.encodeBase64String(tokenBeforeBase.getBytes());
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * MD5加密
	 * 
	 * @param encryptStr
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static final String MD5(String encryptStr) throws UnsupportedEncodingException {
		return DigestUtils.md5Hex(encryptStr.getBytes("UTF-8"));
	}

	/**
	 * 通过当前序列字符串计算下个序列字符串
	 * 
	 * @param noStr
	 * @return
	 */
	public static final String nextSequenceString(String noStr) {

		if (noStr == null || noStr.isEmpty()) {
			throw new NullPointerException("计算序列为空");
		}
		// 序列字符长度
		int strlen = noStr.length();
		// 当前序列编号
		long nowNo = Long.parseLong(noStr);
		// 下个序列长度
		int resultLen = String.valueOf(++nowNo).length();
		StringBuffer sequence = new StringBuffer();
		// 补齐“0”
		for (int i = resultLen; i < strlen; i++) {
			sequence.append(0);
		}
		// 完成拼接
		sequence.append(nowNo);
		return sequence.toString();
	}

	/**
	 * 下划线转换驼峰命名
	 * 
	 * @param name
	 * @return
	 */
	public static String camelName(String name) {

		// 快速检查
		if (name == null || name.isEmpty()) {
			throw new IllegalArgumentException("转换的下划线字符为空");
		}
		// 用下划线将原始字符串分割
		String camels[] = name.split("_");

		if (camels.length > 1) {

			StringBuffer result = new StringBuffer();
			result.append(camels[0]);
			int size = camels.length;
			for (int i = 1; i < size; i++) {
				String camel = camels[i];
				// 跳过原始字符串中开头、结尾的下换线或双重下划线
				if (camel.isEmpty()) {
					continue;
				}
				// 处理真正的驼峰片段
				result.append(camel.substring(0, 1).toUpperCase());
				result.append(camel.substring(1).toLowerCase());
			}
			return result.toString();
		} else {
			return name;
		}
	}

	/**
	 * 驼峰命名转下划线
	 * 
	 * @param name
	 * @return
	 */
	public static String underscoreName(String name) {

		StringBuffer result = new StringBuffer();

		if (name == null || name.isEmpty()) {
			throw new IllegalArgumentException("转换的驼峰字符为空");
		}

		int nameLength = name.length();

		// 循环处理其余字符
		for (int i = 0; i < nameLength; i++) {
			char s = name.charAt(i);
			// 在大写字母前添加下划线
			if (Character.isUpperCase(s)) {
				result.append("_");
			}
			// 其他字符直接转成大写
			result.append(Character.toLowerCase(s));
		}
		return result.toString();
	}

	/**
	 * 获取序列
	 * 
	 * @param str
	 * @param index
	 * @return
	 */
	public static final String getSequenceString(String str, int index) {

		int strlen = str.length();
		return str.substring(strlen - index, strlen);
	}

	public static void main(String[] args) {
		// System.out.println(getToken(getRandomString(15), "18584091017"));
		// System.out.println(Interface.NATIONAL_STANDARD.getValue());
		// System.out.println(getSequenceString("123456789000008999", 5));
		// System.out.println(EncodingUtils.nextSequenceString(getSequenceString("123456789000008999",
		// 5)));
		// System.out.println((1 == Valid.NO.getValue()));
		// System.out.println(underscoreName("helloWord"));
		// System.out.println(camelName("hello_word"));
		System.out.println(nextSequenceString("002800000000000001"));
	}
}
